#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/7/20 11:45
# @Author  : Sun
# @File    : join_room.py
# @Software: PyCharm
import json

import requests

from common.mqtt_connect import MQTTConnect
from common.write_log import WriteLog


class GetRedPacketPlan(MQTTConnect):
    send_gifts_result = None
    _URL = 'https://test-api.live.jjmatch.com:21102/redPacket/getRedPacketPlan'
    _HEADERS = {
        'platform': 'Android-Host',
        'Authorization': None,
        'Content-Type': 'application/json',
        'Accept-Language': 'en-US,en;q=0.8'
    }
    # planType - 1是钻石，3是金币
    _DATA = json.dumps({
        "planType": 3
    })

    log = WriteLog().write_log()

    # 获取红包方案
    def get_red_packet_plan(self):
        """如果Token不为空就不去重复的调用"""
        # 给headers赋值
        if self._HEADERS['Authorization'] is None:
            self._HEADERS['Authorization'] = super().login()
        self.log.info('get_red_packet_plan的Headers：' + str(self._HEADERS))
        r = requests.request(method='POST', url=self._URL, headers=self._HEADERS, data=self._DATA)
        # ensure_ascii置为False，以GBK的形式返回
        self.send_gifts_result = json.dumps(r.json(), ensure_ascii=False, indent=4)
        self.log.info('get_red_packet_plan的请求结果：' + str(self.send_gifts_result))
        return self.send_gifts_result


if __name__ == '__main__':
    GetRedPacketPlan().get_red_packet_plan()